package com.cn.shuangzi.ad.manager;

import android.app.Activity;
import android.util.Log;

import com.bytedance.msdk.adapter.util.Logger;
import com.bytedance.msdk.api.GMAdEcpmInfo;
import com.bytedance.msdk.api.v2.GMAdConstant;
import com.bytedance.msdk.api.v2.GMMediationAdSdk;
import com.bytedance.msdk.api.v2.GMSettingConfigCallback;
import com.bytedance.msdk.api.v2.ad.fullvideo.GMFullVideoAd;
import com.bytedance.msdk.api.v2.ad.fullvideo.GMFullVideoAdLoadCallback;
import com.bytedance.msdk.api.v2.slot.GMAdOptionUtil;
import com.bytedance.msdk.api.v2.slot.GMAdSlotFullVideo;
import com.cn.shuangzi.ad.ADManager;
import com.cn.shuangzi.util.SZUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdFullVideoManager {

    private GMFullVideoAd mFullVideoAd;
    private Activity mActivity;
    private GMFullVideoAdLoadCallback mGMFullVideoAdLoadCallback;
    private int mOrientation; //方向
    private String mAdUnitId; //广告位

    public AdFullVideoManager(Activity activity, GMFullVideoAdLoadCallback fullVideoAdLoadCallback) {
        mActivity = activity;
        mGMFullVideoAdLoadCallback = fullVideoAdLoadCallback;
    }

    public GMFullVideoAd getFullVideoAd() {
        return mFullVideoAd;
    }

    public void laodAdWithCallback(final String adUnitId, final int orientation) {
        this.mOrientation = orientation;
        this.mAdUnitId = adUnitId;

        /**
         * 判断当前是否存在config 配置 ，如果存在直接加载广告 ，如果不存在则注册config加载回调
         */
        if (GMMediationAdSdk.configLoadSuccess()) {
            SZUtil.logError("load ad 当前config配置存在，直接加载广告");
            loadAd(adUnitId, orientation);
        } else {
            SZUtil.logError("load ad 当前config配置不存在，正在请求config配置....");
            ADManager.getInstance().initGMAd();
            GMMediationAdSdk.registerConfigCallback(mSettingConfigCallback); //不用使用内部类，否则在ondestory中无法移除该回调
        }
    }

    private void loadAd(String unitId, int orientation) {
        /**
         * 注：每次加载全屏视频广告的时候需要新建一个GMFullVideoAd，否则可能会出现广告填充问题
         */
        mFullVideoAd = new GMFullVideoAd(mActivity, unitId);

//        Map<String, String> customData = new HashMap<>();
//        customData.put(GMAdConstant.CUSTOM_DATA_KEY_GDT, "gdt custom data");//目前仅支持gdt
        // 其他需要透传给adn的数据。

        GMAdSlotFullVideo adSlotFullVideo = new GMAdSlotFullVideo.Builder()
                .setGMAdSlotGDTOption(GMAdOptionUtil.getGMAdSlotGDTOption().build())
                .setGMAdSlotBaiduOption(GMAdOptionUtil.getGMAdSlotBaiduOption().build())
                .setUserID("user123")//用户id,必传参数
                .setMuted(true)
                .setVolume(0f)
                .setOrientation(orientation)//必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL;
//                .setRewardName("金币") //奖励的名称
//                .setRewardAmount(3)  //奖励的数量
//                .setCustomData(customData) //服务端验证
                .build();
        mFullVideoAd.loadAd(adSlotFullVideo, mGMFullVideoAdLoadCallback);
    }

    public void destroy() {
        if (mFullVideoAd != null) {
            mFullVideoAd.destroy();
        }
        mActivity = null;
        mGMFullVideoAdLoadCallback = null;
        GMMediationAdSdk.unregisterConfigCallback(mSettingConfigCallback); //注销config回调
    }

    /**
     * config回调
     */
    private GMSettingConfigCallback mSettingConfigCallback = new GMSettingConfigCallback() {
        @Override
        public void configLoad() {
            SZUtil.logError("load ad 在config 回调中加载广告");
            loadAd(mAdUnitId, mOrientation);
        }
    };


    //-----------以下方法非必须 ，按需使用--------------

    //打印已经加载广告的信息
    public void printLoadAdInfo() {
        if (mFullVideoAd == null) {
            return;
        }
        /**
         * 获取已经加载的clientBidding ，多阶底价广告的相关信息
         */
        List<GMAdEcpmInfo> gmAdEcpmInfos = mFullVideoAd.getMultiBiddingEcpm();
        if (gmAdEcpmInfos != null) {
            for (GMAdEcpmInfo info : gmAdEcpmInfos) {
                SZUtil.logError("***多阶+client相关信息*** AdNetworkPlatformId" + info.getAdNetworkPlatformId()
                        + "  AdNetworkRitId:" + info.getAdNetworkRitId()
                        + "  ReqBiddingType:" + info.getReqBiddingType()
                        + "  PreEcpm:" + info.getPreEcpm()
                        + "  LevelTag:" + info.getLevelTag()
                        + "  ErrorMsg:" + info.getErrorMsg()
                        + "  request_id:" + info.getRequestId());
            }
        }

        /**
         * 获取实时填充/缓存池中价格最优的代码位信息即相关价格信息，每次只有一个信息
         */
        GMAdEcpmInfo gmAdEcpmInfo = mFullVideoAd.getBestEcpm();
        if (gmAdEcpmInfo != null) {
            SZUtil.logError("***实时填充/缓存池中价格最优的代码位信息*** AdNetworkPlatformId" + gmAdEcpmInfo.getAdNetworkPlatformId()
                    + "  AdNetworkRitId:" + gmAdEcpmInfo.getAdNetworkRitId()
                    + "  ReqBiddingType:" + gmAdEcpmInfo.getReqBiddingType()
                    + "  PreEcpm:" + gmAdEcpmInfo.getPreEcpm()
                    + "  LevelTag:" + gmAdEcpmInfo.getLevelTag()
                    + "  ErrorMsg:" + gmAdEcpmInfo.getErrorMsg()
                    + "  request_id:" + gmAdEcpmInfo.getRequestId());
        }

        /**
         * 获取获取当前缓存池的全部信息
         */
        List<GMAdEcpmInfo> gmCacheInfos = mFullVideoAd.getCacheList();
        if (gmCacheInfos != null) {
            for (GMAdEcpmInfo info : gmCacheInfos) {
                SZUtil.logError("***缓存池的全部信息*** AdNetworkPlatformId" + info.getAdNetworkPlatformId()
                        + "  AdNetworkRitId:" + info.getAdNetworkRitId()
                        + "  ReqBiddingType:" + info.getReqBiddingType()
                        + "  PreEcpm:" + info.getPreEcpm()
                        + "  LevelTag:" + info.getLevelTag()
                        + "  ErrorMsg:" + info.getErrorMsg()
                        + "  request_id:" + info.getRequestId());
            }
        }
    }

    //打印加载失败的adn错误信息
    public void printLoadFailAdnInfo() {
        if (mFullVideoAd == null) {
            return;
        }

        // 获取本次waterfall加载中，加载失败的adn错误信息。
        SZUtil.logError( "reward ad loadinfos: " + mFullVideoAd.getAdLoadInfoList());
    }

    //打印已经展示的广告信息
    public void printSHowAdInfo() {
        if (mFullVideoAd == null) {
            return;
        }
        GMAdEcpmInfo gmAdEcpmInfo = mFullVideoAd.getShowEcpm();
        if (gmAdEcpmInfo == null) {
            return;
        }
        SZUtil.logError( "展示的广告信息 ：adNetworkPlatformName: " + gmAdEcpmInfo.getAdNetworkPlatformName() + "   adNetworkRitId：" + gmAdEcpmInfo.getAdNetworkRitId() + "   preEcpm: " + gmAdEcpmInfo.getPreEcpm());
    }

}
